package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.model.Pagination;
import com.cloudbroker.bcs.common.util.DateUtil;
import com.cloudbroker.bcs.common.util.StringUtil;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.ExtendAppErr;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.PlugsErr;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.type.ExtendAppSortFlag;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.type.PlugsChargeModType;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.ExtendAppBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.result.ExtendAppBuyResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.result.ExtendAppBuyedListData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.result.ExtendAppBuyedListResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.vo.ExtendAppBuyForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.vo.ExtendAppBuyedListDbForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.vo.ExtendAppBuyedListForm;
import com.cloudbroker.bcs.platform.dav.common.constant.SysContants;
import com.cloudbroker.bcs.platform.dav.common.util.SessionHelper;
import com.cloudbroker.bcs.platform.dav.middle.context.HSBCPSBizDataContext;
import com.cloudbroker.bcs.platform.dav.middle.entity.ExtendApp;
import com.cloudbroker.bcs.platform.dav.middle.entity.Plugs;
import com.cloudbroker.bcs.platform.dav.middle.service.BrokerService;
import com.cloudbroker.bcs.platform.dav.middle.service.ExtendAppGroupService;
import com.cloudbroker.bcs.platform.dav.middle.service.ExtendAppService;
import com.cloudbroker.bcs.platform.dav.middle.service.ExtendGroupService;
import com.cloudbroker.bcs.platform.dav.middle.service.PlugsService;

@Service
public class ExtendAppBizServiceImpl implements ExtendAppBizService {
    
    @Resource
    private ExtendAppService extendAppService;
    
    @Resource
    private BrokerService brokerService;
    
    @Resource
    private ExtendAppGroupService extendAppGroupService;
    
    @Resource
    private PlugsService plugsService;
    
    @Resource
    private ExtendGroupService extendGroupService;
    
    /*	public HSBCPSBackVO funcExtendAppSetting(ExtendAppForm form){
    		ExtendAppResult result = new ExtendAppResult();
    		
    		ExtendApp app = new ExtendApp();
    		app.setExtend_app_id(Long.valueOf(form.getExtend_app_id()));
    		
    		int count = extendAppService.count(app);
    		if(count == 0){
    			return VOUtil.genErrorResult(ExtendAppErr.EXTEND_APP_NOT_EXIST, ExtendAppResult.class);
    		}
    		
    		app.setNeed_login(Integer.valueOf(form.getNeed_login()));
    		app.setSort(Integer.parseInt(form.getSort()));
    		app.setPersonal_attr(StringUtil.isBlank(form.getPersonal_attr()) ? "[]" : form.getPersonal_attr());
    		
    		extendAppService.update(app);
    		return VOUtil.setSuccessResult(result);
    	}*/
    
    @Override
    public HSBCPSBackVO funcExtendAppBuy(ExtendAppBuyForm form) {
        ExtendAppBuyResult result = new ExtendAppBuyResult();
        int day = 0;
        int count = 0;
        
        Plugs p = new Plugs();
        p.setPlugs_id(Long.valueOf(form.getPlugs_id()));
        p.setPlugs_status(1);
        List<Plugs> list = plugsService.query(p);
        if (list == null || list.isEmpty()) {
            return VOUtil.genErrorResult(PlugsErr.PLUGS_NOT_EXIST, ExtendAppBuyResult.class);
        }
        
        Plugs plugs = list.get(0);
        if (PlugsChargeModType.BYDAY.getVal().equals(plugs.getCharge_mod())) {
            if (StringUtil.isEmpty(form.getDay()) || Integer.parseInt(form.getDay()) <= 0) {
                return VOUtil.genErrorResult(ExtendAppErr.EXTEND_APP_CHARGE_BY_DAY_ERROR, ExtendAppBuyResult.class);
            }
            day = Integer.parseInt(form.getDay());
        } else if (PlugsChargeModType.BYCOUNT.getVal().equals(plugs.getCharge_mod())) {
            if (StringUtil.isEmpty(form.getCount()) || Integer.parseInt(form.getCount()) <= 0) {
                return VOUtil.genErrorResult(ExtendAppErr.EXTEND_APP_CHARGE_BY_COUNT_ERROR,
                        ExtendAppBuyResult.class);
            }
            count = Integer.parseInt(form.getCount());
        }
        
        ExtendApp app = new ExtendApp();
        app.setBroker_id(HSBCPSBizDataContext.getBrokerId());
        app.setTenant_id(HSBCPSBizDataContext.getTenantId());
        Long maxSort = extendAppService.getMaxSort(app);
        app.setPlugs_id(Long.valueOf(form.getPlugs_id()));
        int existCount = extendAppService.count(app);
        if (existCount > 0) {
            return VOUtil.genErrorResult(ExtendAppErr.EXTEND_APP_BUYED, ExtendAppBuyResult.class);
        }
        
        app.setSort(maxSort);
        app.setPrice(plugs.getDiscount_price());
        app.setName(plugs.getName());
        app.setCreater(plugs.getCreater());
        app.setExtend_app_status(1);
        app.setLink(plugs.getLink());
        app.setCharge_mod(plugs.getCharge_mod());
        app.setUsed(0);
        app.setStart_time(new Date());
        app.setValid_time(DateUtil.getDateByAddDay(new Date(), day));
        app.setCount(count);
        app.setTerm_type(plugs.getTerm_type());
        extendAppService.save(app);
        //清空缓存
        SessionHelper.getInstance().clearNewExtendAppsByBrokerId(HSBCPSBizDataContext.getBrokerId(), "");
        //更改购买次数 （处理并发更新）
        plugsService.updateBuyCount(plugs);
        
        return VOUtil.setSuccessResult(result);
    }
    
    public ExtendAppBuyedListResult funcExtendAppBuyedList(ExtendAppBuyedListForm form) {
        ExtendAppBuyedListResult result = new ExtendAppBuyedListResult();
        
        Integer request_num = StringUtil.isBlank(form.getRequest_num())
                || Integer.parseInt(form.getRequest_num()) <= 0 ? SysContants.DEFAULT_REQUSET_NUM : Integer
                .parseInt(form.getRequest_num());
        Integer page_no = StringUtil.isBlank(form.getPage_no()) || Integer.parseInt(form.getPage_no()) <= 0 ? 1
                : Integer.parseInt(form.getPage_no());
        
        ExtendAppBuyedListDbForm selectPara = new ExtendAppBuyedListDbForm();
        if (!StringUtil.isEmpty(form.getName())) {
            selectPara.setName(form.getName());
        }
        if (!StringUtil.isEmpty(form.getPlugs_type_id())) {
            selectPara.setPlugs_type_id(form.getPlugs_type_id());
        }
        if(!StringUtil.isEmpty(form.getTerm_type_num())){
            selectPara.setTerm_type_num(form.getTerm_type_num());
        }
        
        if(!StringUtil.isEmpty(form.getExtend_app_status())){
            selectPara.setExtend_app_status(form.getExtend_app_status());
        }
        
        if(StringUtil.isEmpty(form.getSort_mark_flag()) || ExtendAppSortFlag.DEFAULT_SORT.getVal() == Integer.parseInt(form.getSort_mark_flag())){
            selectPara.setDefault_sort("");
        } else if(ExtendAppSortFlag.VALID_TIME.getVal() == Integer.parseInt(form.getSort_mark_flag())){
            selectPara.setInvalid_time_sort("");
        } else if(ExtendAppSortFlag.HOT_SORT.getVal() == Integer.parseInt(form.getSort_mark_flag())){
            selectPara.setHot_sort("");
        }
        selectPara.setBroker_id(HSBCPSBizDataContext.getBrokerId().toString());
        selectPara.setTenant_id(HSBCPSBizDataContext.getTenantId());
        
        Pagination page = new Pagination();
        page.setPage_no(page_no);
        page.setPage_size(request_num);
        
        List<ExtendAppBuyedListData> list = extendAppService.listWithPlugs(selectPara, page);
        
        result.setPage_no(page.getPage_no());
        result.setPage_size(page.getPage_size());
        result.setTotal_item_num(page.getTotal_item_num());
        result.newDataList().addAll(list);
        
        return VOUtil.setSuccessResult(result);
    }
}
